package com.zsx.shopping.ware.listener;

import com.rabbitmq.client.Channel;
import com.zsx.shopping.ware.service.OrderStockTaskService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;

@Service
public class StockReleaseListener {

    @Resource
    private OrderStockTaskService orderStockTaskService;

    @RabbitListener(queues = "stock.release.stock.queue")
    public void releaseStock(Long orderId, Message message, Channel channel) throws IOException {
        MessageProperties properties = message.getMessageProperties();
        orderStockTaskService.releaseStock(orderId);
        try {
            channel.basicAck(properties.getDeliveryTag(), false);
        } catch (IOException e) {
            channel.basicReject(properties.getDeliveryTag(), true);
        }
    }
}
